package com.scd.socket;

import java.io.File;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.scd.util.AesUtil;
import com.scd.util.Base64Util;
import com.scd.util.FileUtil;
import com.scd.util.RsaUtil;
import com.scd.vo.ClientInfo;

public class ClientTest {
	
	public static final String KEY_PATH = "C:/Users/CD/Desktop/RSA";
	
	public static final String SERVER_PATH = KEY_PATH + File.separator + "server";
	
	public static final String CLIENT_PATH = KEY_PATH + File.separator + "client";
	
	public static final String UTF8_ENCODE = "UTF-8";

	public static ClientInfo createClientData() throws Exception {
		ClientInfo clientInfo = new ClientInfo();
		
		// aeskey 使用 RSA加密	使用服务器端公钥
		String aesKey = "1111111111111111";
		clientInfo.setAeskey(encryptStr(aesKey));
		// 图片信息通过 AES 加密
		String imgPath = "";
		clientInfo.setPicData(aesImg(imgPath, aesKey));
		// 用户信息 AES 加密
		String input = "name:chengdu";
		clientInfo.setUserInfo(aesStr(input, aesKey));
		// 获取客户端公钥
		clientInfo.setClientPub(getClientPub());
//		JSONObject jsonObject = (JSONObject) JSON.toJSON(clientInfo);
		return clientInfo;
	}
	
	/**
	 * 服务器端公钥加密 aes key
	 * @param input
	 * @return
	 * @throws Exception
	 */
	public static String encryptStr(String input) throws Exception {
		
		byte[] spubkey = FileUtil.readByteArrayByfpath(SERVER_PATH + File.separator + "public.conf");
		byte[] inputData = input.getBytes(UTF8_ENCODE);
		byte[] rsaEData = RsaUtil.encryptByPublicKey(inputData, spubkey);
		return Base64Util.encryptBASE64(rsaEData);
	}
	
	public static String aesImg(String imgPath, String aesKey) throws Exception {
    	String picBase64 = Base64Util.getPicBASE64(imgPath);
    	// 对 picBase64图片加密
    	byte[] aesPic = AesUtil.aesEncrypt(picBase64, aesKey);
    	// 转成Base64字符串传输
    	return  Base64Util.encryptBASE64(aesPic);
	}
	
	public static String aesStr(String input, String aesKey) throws Exception {
		byte[] aesStr = AesUtil.aesEncrypt(input, aesKey);
		return Base64Util.encryptBASE64(aesStr);
	}
	
	public static String getClientPub() throws Exception {
		byte[] cpubkey = FileUtil.readByteArrayByfpath(CLIENT_PATH + File.separator + "public.conf");
		return Base64Util.encryptBASE64(cpubkey);
	}
	
	
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		createClientData();
	}

}
